//
//  PieView.m
//  PieDrawing
//
//  Created by Yang on 10/14/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SC_PieView.h"


@implementation SC_PieView

#define K_PI 3.1415
#define KDGREED(x) ((x)  * K_PI * 2)

static float ChartMarginLeftAndRight = 10.0f;

@synthesize spaceHeight, scaleY;
@synthesize dotArray;
@synthesize delegate;
 
- (CGGradientRef)newGradientWithColors:(UIColor**)colors locations:(CGFloat*)locations
								 count:(int)count {
	CGFloat* components = malloc(sizeof(CGFloat)*4*count);
	for (int i = 0; i < count; ++i) {
		UIColor* color = colors[i];
		size_t n = CGColorGetNumberOfComponents(color.CGColor);
		const CGFloat* rgba = CGColorGetComponents(color.CGColor);
		if (n == 2) {
			components[i*4] = rgba[0];
			components[i*4+1] = rgba[0];
			components[i*4+2] = rgba[0];
			components[i*4+3] = rgba[1];
		} else if (n == 4) {
			components[i*4] = rgba[0];
			components[i*4+1] = rgba[1];
			components[i*4+2] = rgba[2];
			components[i*4+3] = rgba[3];
		}
	}
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGColorSpaceRef space = CGBitmapContextGetColorSpace(context);
	CGGradientRef _gradient = CGGradientCreateWithColorComponents(space, components, locations, count);
	free(components);
	return _gradient;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
		self.backgroundColor = [UIColor clearColor];
		
		//spaceHeight = 60;
		spaceHeight = frame.size.width / 6.0f;
		scaleY = 0.4;
		ChartMarginLeftAndRight = frame.size.width / 8.0f;
		dataArray = [[NSMutableArray alloc] init];
		dotArray = [[NSMutableArray alloc] init];
		
		
		
    }
    return self;
}
- (void)resetFrame:(CGRect )frame {
	spaceHeight = frame.size.width / 6.0f;
	scaleY = 0.4;
	ChartMarginLeftAndRight = frame.size.width / 8.0f;
	[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect 
{
	[dotArray removeAllObjects];
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetAllowsAntialiasing(context, TRUE);
	
	float sum = 0;
	
	for(int j=0;j< [dataArray count]; j++)
	{
		sum	 += [(SC_PieChartData *)[dataArray objectAtIndex:j] value];
	}
	
	
	CGContextMoveToPoint(context, self.bounds.size.width / 2, self.bounds.size.height / 2 / scaleY);
	
	float currentangel = 0;
	
	
	//饼图
	CGContextSaveGState(context);
	CGContextScaleCTM(context, 1.0, scaleY);
	
	currentangel = 0;
	
	for(int i = 0; i< [dataArray count]; i++)
	{
		CGMutablePathRef path = CGPathCreateMutable();
		
		
		float startAngle = KDGREED(currentangel);
		
		currentangel += [(SC_PieChartData *)[dataArray objectAtIndex:i] value] / sum;
		
		float endAngle = KDGREED(currentangel);
		
		// 计算标注点的位置
		
		float tempCenterAngle = (endAngle - startAngle) / 2.0f + startAngle;
		float dotX = self.bounds.size.width / 2 + (self.bounds.size.width / 2 - ChartMarginLeftAndRight) * cos(tempCenterAngle);
		float dotY = self.bounds.size.height / 2 + (self.bounds.size.width / 2 - ChartMarginLeftAndRight)*scaleY * sin(tempCenterAngle);
		//NSLog(@"dotX : %.0f",dotX);
		//NSLog(@"dotY : %.0f",dotY);
		[dotArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:dotX],@"x",[NSNumber numberWithFloat:dotY],@"y",nil]];
		//绘制上面的扇形
		CGContextMoveToPoint(context, self.bounds.size.width / 2, self.bounds.size.height / 2 / scaleY);
		
		//[[(SC_PieChartData *)[dataArray objectAtIndex:i] color] setFill];
		
		[[UIColor colorWithWhite:1.0 alpha:0.8] setStroke];
		
		CGContextAddArc(context, self.bounds.size.width / 2, self.bounds.size.height / 2 / scaleY, self.bounds.size.width / 2 - ChartMarginLeftAndRight, startAngle, endAngle, 0);
		
		//CGContextClosePath(context);
		//CGContextDrawPath(context, kCGPathFill);
		//CGContextClosePath(context);
		//CGContextFillPath(context);
		//CGContextDrawPath(context, kCGPathFill);
		
		CGGradientRef myGradient;
		CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
		
		CGFloat colors1[] =
		{
			60.0 / 255.0, 160.0 / 255.0, 0.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 86.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 96.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 106.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 116.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 126.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 136.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 146.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 156.0 / 255.0, 1.00,
			199.0 / 255.0, 236.0 / 255.0, 79.0 / 255.0, 1.00,
			//69.0 / 255.0, 168.0 / 255.0, 0.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 76.0 / 255.0, 1.00,
//			69.0 / 255.0, 168.0 / 255.0, 0.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 76.0 / 255.0, 1.00,
//			69.0 / 255.0, 168.0 / 255.0, 0.0 / 255.0, 1.00,
//			148.0 / 255.0, 203.0 / 255.0, 76.0 / 255.0, 1.00,
//			69.0 / 255.0, 168.0 / 255.0, 0.0 / 255.0, 1.00,
		};
		CGFloat colors2[] =
		{
//			142.0 / 255.0, 213.0 / 255.0, 247.0 / 255.0, 1.00,
//			76.0 / 255.0, 118.0 / 255.0, 222.0 / 255.0, 1.00,
//			142.0 / 255.0, 213.0 / 255.0, 247.0 / 255.0, 1.00,
//			76.0 / 255.0, 118.0 / 255.0, 222.0 / 255.0, 1.00,
//			142.0 / 255.0, 213.0 / 255.0, 247.0 / 255.0, 1.00,
//			76.0 / 255.0, 118.0 / 255.0, 222.0 / 255.0, 1.00,
//			142.0 / 255.0, 213.0 / 255.0, 247.0 / 255.0, 1.00,
			17.0 / 255.0, 116.0 / 255.0, 233.0 / 255.0, 1.00,
			127.0 / 255.0, 229.0 / 255.0, 254.0 / 255.0, 1.00,
		};
		CGFloat colors3[] =
		{
//			239.0 / 255.0, 212.0 / 255.0, 77.0 / 255.0, 1.00,
//			228.0 / 255.0, 125.0 / 255.0, 55.0 / 255.0, 1.00,
//			239.0 / 255.0, 212.0 / 255.0, 77.0 / 255.0, 1.00,
//			228.0 / 255.0, 125.0 / 255.0, 55.0 / 255.0, 1.00,
//			239.0 / 255.0, 212.0 / 255.0, 77.0 / 255.0, 1.00,
//			228.0 / 255.0, 125.0 / 255.0, 55.0 / 255.0, 1.00,
//			239.0 / 255.0, 212.0 / 255.0, 77.0 / 255.0, 1.00,
			255.0 / 255.0, 122.0 / 255.0, 3.0 / 255.0, 1.00,
			253.0 / 255.0, 223.0 / 255.0, 20.0 / 255.0, 1.00,
		};
		if (i == 0) {
			myGradient = CGGradientCreateWithColorComponents(rgb, colors1, NULL, sizeof(colors1)/(sizeof(colors1[0])*4));
		}
		else if (i == 1) {
			myGradient = CGGradientCreateWithColorComponents(rgb, colors2, NULL, sizeof(colors2)/(sizeof(colors2[0])*4));
		}
		else {
			myGradient = CGGradientCreateWithColorComponents(rgb, colors3, NULL, sizeof(colors3)/(sizeof(colors3[0])*4));
		}

		

		
		CGColorSpaceRelease(rgb);
		
		CGPoint start,end;
		start = end = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2 / scaleY);
		
		//CGContextDrawPath(context, kCGPathFill);
		CGContextSaveGState(context);
		CGContextClip(context);
		

		//UIColor* colors[] = {[UIColor colorWithRed:250/255.0 green:175/255.0 blue:64/255.0 alpha:1.0], [UIColor colorWithRed:240/255.0 green:90/255.0 blue:40/255.0 alpha:1.0]};
		//UIColor* colors1[] = {[UIColor colorWithRed:148/255.0 green:203/255.0 blue:76/255.0 alpha:1.0], [UIColor colorWithRed:69.0/255.0 green:168.0/255.0 blue:0.0/255.0 alpha:1.0]};
//		UIColor* colors2[] = {[UIColor colorWithRed:142/255.0 green:213/255.0 blue:147/255.0 alpha:1.0], [UIColor colorWithRed:76.0/255.0 green:118.0/255.0 blue:222.0/255.0 alpha:1.0]};
//		UIColor* colors3[] = {[UIColor colorWithRed:239/255.0 green:211/255.0 blue:77/255.0 alpha:1.0], [UIColor colorWithRed:228.0/255.0 green:125.0/255.0 blue:55.0/255.0 alpha:1.0]};
//		UIColor *colors1[] = {[UIColor blueColor],[UIColor yellowColor]};
//		UIColor *colors2[] = {[UIColor purpleColor],[UIColor redColor]};
//		UIColor *colors3[] = {[UIColor greenColor],[UIColor orangeColor]};
//		CGGradientRef _gradient;// = [self newGradientWithColors:colors locations:nil count:2];
//		if (i == 0) {
//			_gradient = [self newGradientWithColors:colors1 locations:nil count:2];
//		}
//		else if (i == 1) {
//			_gradient = [self newGradientWithColors:colors2 locations:nil count:2];
//		}
//		else {
//			_gradient = [self newGradientWithColors:colors3 locations:nil count:2];
//		}
//
//		CGContextDrawLinearGradient(context, _gradient, CGPointZero, CGPointMake(rect.origin.x, rect.origin.y+rect.size.height), kCGGradientDrawsAfterEndLocation);
//		CGGradientRelease(_gradient);
		
		
		CGContextDrawRadialGradient(context, myGradient, start, 0, end, self.bounds.size.width / 2 - ChartMarginLeftAndRight, kCGGradientDrawsBeforeStartLocation);
		CGContextRestoreGState(context);
		//CGContextClosePath(context);
		CGContextFillPath(context);
		CGContextDrawPath(context, kCGPathFill);

		
		
		
//		context = UIGraphicsGetCurrentContext();
//		CGContextSetAllowsAntialiasing(context, TRUE);
//		CGContextSaveGState(context);
 
		//context = UIGraphicsGetCurrentContext();
		
		
		//绘制侧面
		float starx = cos(startAngle) * (self.bounds.size.width / 2 - ChartMarginLeftAndRight)  +self.bounds.size.width / 2;
		float stary = sin(startAngle) * (self.bounds.size.width / 2 - ChartMarginLeftAndRight) + self.bounds.size.height / 2;
		
		float endx = cos(endAngle) * (self.bounds.size.width / 2 - ChartMarginLeftAndRight)  +self.bounds.size.width / 2;
		float endy = sin(endAngle) * (self.bounds.size.width / 2 - ChartMarginLeftAndRight) + self.bounds.size.height / 2;
		
		float endy1 = endy + spaceHeight;

		
		if(endAngle < K_PI)
		{
		}
		
		//只有弧度《 3.14 的才会画前面的厚度
		else if(startAngle < K_PI)
		{
			endAngle = K_PI;
			endx = ChartMarginLeftAndRight;
			endy1 = self.bounds.size.height / 2+spaceHeight;
		}
		
		else
			continue;
		
		
		//CGContextSetBlendMode(context, kCGBlendModeMultiply);
		
		
		//绘制厚度
		//CGMutablePathRef path = CGPathCreateMutable();
		CGPathMoveToPoint(path, nil, starx, stary);
		CGPathAddArc(path, nil, self.bounds.size.width / 2, self.bounds.size.height / 2 / scaleY, self.bounds.size.width / 2 - ChartMarginLeftAndRight, startAngle, endAngle, 0);
		CGPathAddLineToPoint(path, nil, endx, endy1);
		
		CGPathAddArc(path, nil, self.bounds.size.width / 2, self.bounds.size.height / 2 / scaleY+ spaceHeight, self.bounds.size.width / 2 - ChartMarginLeftAndRight, endAngle, startAngle, 1);
		CGContextAddPath(context, path);
		//CGContextClosePath(context);
		
		CGContextSaveGState(context);
		CGContextClip(context);
		CGContextDrawLinearGradient(context, myGradient, CGPointMake(starx, stary), CGPointMake(endx, endy1), kCGGradientDrawsBeforeStartLocation);
		CGContextDrawLinearGradient(context, myGradient, CGPointMake(starx, stary), CGPointMake(endx, endy1), kCGGradientDrawsAfterEndLocation);
		//CGContextDrawRadialGradient(context, myGradient, start, self.bounds.size.width / 2 - ChartMarginLeftAndRight, CGPointMake(end.x, end.y + spaceHeight), self.bounds.size.width / 2 - ChartMarginLeftAndRight, kCGGradientDrawsBeforeStartLocation);
		CGContextRestoreGState(context);
		
//		[[[dataArray objectAtIndex:i] color] setFill];
//		[[UIColor colorWithWhite:0.9 alpha:1.0] setStroke];
//		
//		CGContextDrawPath(context, kCGPathFill);
//		
//		[[UIColor colorWithWhite:0.1 alpha:0.4] setFill];
//		CGContextAddPath(context, path);
//		CGContextDrawPath(context, kCGPathFill);
		

		
		
//		CGContextSaveGState(context);
//		CGContextClip(context);
//		CGContextDrawRadialGradient(context, myGradient, start, 0, end, self.bounds.size.width / 2 - ChartMarginLeftAndRight, kCGGradientDrawsBeforeStartLocation);
//		CGContextRestoreGState(context);
		
		
//		CGContextClip(context);
//		
//		UIColor* colors[] = {[(SC_PieChartData *)[dataArray objectAtIndex:i] color], [UIColor colorWithRed:240/255.0 green:90/255.0 blue:40/255.0 alpha:1.0]};
//		CGGradientRef _gradient = [self newGradientWithColors:colors locations:nil count:2];
//		CGContextDrawLinearGradient(context, _gradient, CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2 / scaleY), CGPointMake(endx, endy), kCGGradientDrawsBeforeStartLocation);
//		CGGradientRelease(_gradient);
//		
//		CGContextRestoreGState(context);
		

		
	}
	[delegate needReloadMarkersFor:dataArray Dots:dotArray];
	
	
	//整体渐变
//	CGFloat componets [] = {0.0, 0.0, 0.0, 0.5,0.0,0.0,0.0,0.1};
//	
//	CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
//	
//	CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, componets, nil, 2);
//	
//	CGPoint centerPoint = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2 / scaleY);
//	
//	CGContextDrawRadialGradient(context, gradient, centerPoint, 0, centerPoint, self.bounds.size.width / 2 - ChartMarginLeftAndRight, 0 );
//
//
//	CFRelease(colorspace);
//	CGGradientRelease(gradient);
//	
//	
//	CGContextRestoreGState(context);
	
	//绘制文字
	
//	for(int i = 0; i< [valueArr count]; i++)
//	{
//		float origionx = 50 ;
//		float origiony = i * 30 + 200;
//		
//		[[colorArr objectAtIndex:i %  [valueArr count]] setFill];
//		
//		CGContextFillRect(context, CGRectMake(origionx, origiony, 20, 20));
//		CGContextDrawPath(context, kCGPathFill);
//		
//		
//		if(i< [titleArr count])
//		{
//			NSString *title = [ titleArr objectAtIndex:i];
//			[title drawAtPoint:CGPointMake(origionx + 50, origiony) withFont:[UIFont systemFontOfSize:16]];
//		}
//		
//	}
	
}
- (void)resetData:(NSArray *)array {
	[dataArray setArray:array];
	[self setNeedsDisplay];
}

- (void)dealloc {
    [super dealloc];
}



@end
